import { getRepository } from 'typeorm';
import { NextFunction, Request, Response } from 'express';
import { Cat } from '../entity/Cat';
import { successRes, failRes } from '../utils/response';

var request = require('request');
export class CatController {
  private catRepository = getRepository(Cat);
  async list(request: Request, response: Response, next: NextFunction) {
    try {
      const { pageNo, pageSize, name, createdDate } = request.query;
      var data = [];
      var total = 0;
      let queryBuilder = this.catRepository.createQueryBuilder('cat');
      if (name) {
        queryBuilder = queryBuilder.andWhere('cat.name like :name', { name: '%' + name + '%' });
      }

      if (createdDate) {
        queryBuilder = queryBuilder.andWhere(`cat.createdDate >= :after and cat.createdDate <= :before`, {
          after: createdDate.split(' ')[0] + ' 00:00:00',
          before: createdDate.split(' ')[0] + ' 23:59:59',
        });
      }

      queryBuilder = queryBuilder.skip((pageNo - 1) * pageSize).take(pageSize);
      queryBuilder = queryBuilder.orderBy('cat.createdDate', 'DESC');
      // const sql = await queryBuilder.getSql();
      // console.log('sql', sql);
      data = await queryBuilder.getMany();
      total = await queryBuilder.getCount();

      let result = {
        current: pageNo,
        records: data,
        size: pageSize,
        total: total,
      };
      return successRes(result);
    } catch (error) {
      return failRes(error);
    }
  }

  async one(request: Request, response: Response, next: NextFunction) {
    try {
      console.log('request.query.id', request.query);
      let result = await this.catRepository.findOne(request.query.id);
      return successRes(result);
    } catch (error) {
      return failRes(error);
    }
  }

  async add(request: Request, response: Response, next: NextFunction) {
    try {
      const toSave = request.body;
      let obj = await this.catRepository.save(toSave);
      if (obj.id) {
        return successRes('添加成功');
      } else {
        return failRes('添加失败');
      }
    } catch (error) {
      return failRes(error);
    }
  }
  async edit(request: Request, response: Response, next: NextFunction) {
    try {
      const toSave = request.body;
      let result = await this.catRepository.findOne(toSave.id);
      if (result) {
        await this.catRepository.save(toSave);
        return successRes('编辑成功');
      } else {
        return failRes({}, 500, '未找到该记录');
      }
    } catch (error) {
      return failRes(error);
    }
  }

  async remove(request: Request, response: Response, next: NextFunction) {
    try {
      let newsToRemove = await this.catRepository.findOne(request.query.id);
      if (newsToRemove) {
        await this.catRepository.remove(newsToRemove);
        return successRes({}, '删除成功');
      } else {
        return failRes({}, 500, '未找到该记录');
      }
    } catch (error) {
      return failRes(error);
    }
  }
  async websiteCatList(request: Request, response: Response, next: NextFunction) {
    try {
      let result = await this.catRepository.find({
        where: {
          status: 1,
        },
        order: {
          createdDate: 'DESC',
        },
      });
      return successRes(result);
    } catch (error) {
      return failRes(error);
    }
  }
  async websiteCatDetail(request: Request, response: Response, next: NextFunction) {
    try {
      console.log('request.query.id', request.query);
      let result = await this.catRepository.findOne(request.query.id);
      return successRes(result);
    } catch (error) {
      return failRes(error);
    }
  }
  async testJeecg(req: Request, response: Response, next: NextFunction) {
    try {
      request('https://t1.yncce.cn/jeecg-boot/gunsApi/app/bootpage', function (err, response, body) {
        console.log(`response`, response.body);
      });
    } catch (error) {
      return failRes(error);
    }
  }
}
